package com.study.service;

import com.study.entity.Account;
import com.study.mapper.AccountDao;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;

@Service("accountService")
public class AccountServiceImpl implements AccountService {

    @Resource
    private AccountDao accountDao;

    @Transactional//事务
    public void transferAccounts(int fromUserId, int toUserId, float account) {
        Account fromUserAccount = accountDao.getOne(fromUserId);
        fromUserAccount.setBalance(fromUserAccount.getBalance() - account);
        accountDao.save(fromUserAccount); // fromUser扣钱

        Account toUserAccount = accountDao.getOne(toUserId);
        toUserAccount.setBalance(toUserAccount.getBalance() + account);
        //假设转账的时候假如出现异常，业务类或业务方法中没有使用@Transactional控制事务，则会出现钱转出了，收钱人没有收到的情况
        int zero = 1 / 0;  //这里我们先把这个异常注释掉，稍后我们再打开
        accountDao.save(toUserAccount); // toUser加钱
    }
}
